package com.cy.db.sys.service.impl;

import com.cy.db.common.exception.ServiceException;
import com.cy.db.common.pojo.PageObject;
import com.cy.db.common.vo.CheckBox;
import com.cy.db.sys.dao.SysRoleDao;
import com.cy.db.sys.dao.SysRoleMenuDao;
import com.cy.db.sys.dao.SysUserRoleDao;
import com.cy.db.sys.entity.SysRole;
import com.cy.db.sys.service.SysLogService;
import com.cy.db.sys.service.SysRoleService;
import com.cy.db.sys.vo.SysRoleMenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Optional;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Override
    public PageObject<SysRole> findPageObjects(String username, Long pageCurrent) {
        //1.验证参数的有效性
        if(pageCurrent == null || pageCurrent<1){
            throw new IllegalArgumentException("当前页码值无效");
        }
//        Optional.ofNullable(pageCurrent).orElseThrow(()->{
//            return new IllegalArgumentException("当前页码值无效");
//        });
        //2.统计总记录数并进行校验
       long rowCount = sysRoleDao.getRowCount(username);
       if(rowCount==0){
           throw new ServiceException("没有找到对应的记录");
       }
        //3.基于条件查询当前页记录(pageSize定义为2)
        //3.1定义pageSize
        int pageSize = 2;
        //3.2计算startIndex
        long startIndex = (pageCurrent-1)*pageSize;
        //3.3执行当前数据的查询操作
        List<SysRole> records = sysRoleDao.findPageObjects(username,startIndex,pageSize);
        return new PageObject<>(rowCount,records,pageSize,pageCurrent);
    }

    @Override
    public int deleteObject(Integer id) {
        //1.验证数据合法性
        if(id == null || id < 0){
            throw new IllegalArgumentException("请先选着");
        }
        //2.基于id删除关系数据
        sysRoleMenuDao.deleteObjectsByMenuId(id);
        sysUserRoleDao.deleteObjectsByRoleId(id);
        //3.删除角色自身
        int row = sysRoleDao.deleteObjectId(id);
        if(row == 0){
            throw new ServiceException("此记录可能已经不存在");
        }
        //4.返回结果
        return row;
    }

    @Override
    public int saveObject(SysRole entity, Integer[] menuIds) {
        //1.校验
        if(entity == null){
            throw new IllegalArgumentException("保存对象不能为空");
        }
        if(StringUtils.isEmpty(entity.getName())){
            throw new IllegalArgumentException("角色名字不能为空");
        }
        if(menuIds == null || menuIds.length == 0){
            throw new IllegalArgumentException("必须为角色分配权限");
        }
        //2.添加关系数据
        sysRoleMenuDao.insertObjects(entity.getId(),menuIds);
        //3.添加自身数据
        int row = sysRoleDao.insertObject(entity);
        return row;
    }

    @Override
    public SysRoleMenuVO findObjectById(Integer id) {
        //1.参数校验
        if(id == null || id < 0){
            throw new IllegalArgumentException("id的值不合法");
        }
        //2.执行查询
        SysRoleMenuVO result = sysRoleDao.findObjectById(id);
        //3.验证结果并返回
        if(result == null){
            throw new ServiceException("此记录不存在");
        }
        return result;
    }

    @Override
    public int updateObject(SysRole entity,Integer[] menuIds) {
        //1.参数校验
        if(entity == null){
            throw new IllegalArgumentException("修改的对象不能为空!");
        }
        if(menuIds == null || menuIds.length == 0){
            throw new IllegalArgumentException("必须为角色分配权限!");
        }
        //2.更新角色自身信息
        int row = sysRoleDao.updateObject(entity);
        if(row == 0){
            throw new IllegalArgumentException("对象可能已经不存在!");
        }
        //3.更新角色菜单关系数据
        //3.1 删除原有的关系数据
        sysRoleMenuDao.deleteObjectsByMenuId(entity.getId());
        //3.2 添加新的关系数据
        sysRoleMenuDao.insertObjects(entity.getId(), menuIds);
        return row;
    }

    @Override
    public List<CheckBox> findObjects() {
        return sysRoleDao.findObjects();
    }
}
